package com.flycent.fanbao.ui;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.flycent.fanbao.App;
import com.flycent.fanbao.R;
import com.flycent.fanbao.adapter.AbsViewHolderAdapter;
import com.flycent.fanbao.bean.Custom;
import com.flycent.fanbao.bean.Custom.CustomList;
import com.flycent.fanbao.ui.view.SimpleTitleBar;

/**
 * @author fbl
 *  私人定制
 */
public class PrivateCustomActivity extends BackableTitleBarActivity{
	@ViewInject(id = R.id.private_custom_list)  ListView mCustomList;
	
	
	private List<Custom> mCustoms ;
	private List<CustomList> mCustomLists = new ArrayList<CustomList>();
	private GridAdapter mGridAdapter;
	private PrivateCustomAdapter mAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.activity_private_custom);
		FinalActivity.initInjectedView(this);
		getTitleBar().setTitle("私人定制");
		
		mCustoms  = new ArrayList<Custom>();
		
		for (int i = 0; i < 5; i++) {
			mCustomLists = new ArrayList<CustomList>();
			for (int j = 0; j < i+3; j++) {
				mCustomLists.add(j, new CustomList(false,"sadfa"));
			}
			mCustoms.add(new Custom("item"+i,mCustomLists));
		}
		
		mAdapter = new PrivateCustomAdapter(this, R.layout.item_custom_list);
		
		mCustomList.setAdapter(mAdapter);
		mAdapter.update(mCustoms);
	}
	
	public class PrivateCustomAdapter extends AbsViewHolderAdapter<Custom>{

		public PrivateCustomAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(final int pos, final Custom itemData) {
			TextView Text1 = getViewFromHolder(R.id.pos_text);
			Text1.setText(itemData.getSs());
			
			GridView mGrid = getViewFromHolder(R.id.gridView);
			
			if(itemData.getmList().size() != 0 ){
				mGrid.setVisibility(View.VISIBLE);
				mGridAdapter = new GridAdapter(PrivateCustomActivity.this, R.layout.item_grid_image);
				mGrid.setAdapter(mGridAdapter); 
				mGridAdapter.update(itemData.getmList());
				
			}else {
				mGrid.setVisibility(View.GONE);
			}
			
			mGrid.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent,
						View view, int position, long id) {
					if( itemData.getmList().get(position).isCheck()) {
						
						mCustoms.get(pos).getmList().get(position).setCheck(false);
//						itemData.getmList().get(position).setCheck(false);
					}else {
						for (int i = 0; i < mCustoms.get(pos).getmList().size(); i++) {
							mCustoms.get(pos).getmList().get(i).setCheck(false);
						}
						
						mCustoms.get(pos).getmList().get(position).setCheck(true);
//						itemData.getmList().get(position).setCheck(true);
					}
//					mGridAdapter.update(itemData.getmList());
					mAdapter.update(mCustoms);
				}
			});
		}
	}
	
	public class GridAdapter extends AbsViewHolderAdapter<CustomList>{

		public GridAdapter(Context context, int layoutRes) {
			super(context, layoutRes);
		}

		@Override
		protected void bindData(int pos, CustomList itemData) {
			ImageView image1 = getViewFromHolder(R.id.round_side_bg);
//			ImageView image2 = getViewFromHolder(R.id.round_image);
			ImageView image3 = getViewFromHolder(R.id.round_check_image);
			
			if( itemData.isCheck()){
				image1.setVisibility(View.VISIBLE);
				image3.setVisibility(View.VISIBLE);
			}else {
				image1.setVisibility(View.INVISIBLE);
				image3.setVisibility(View.INVISIBLE);			
			}
		}
		
	}
	
	
	@Override
	protected void onTitleBarCreated(SimpleTitleBar titleBar) {
		super.onTitleBarCreated(titleBar);
		titleBar.setRightButtonText("确定");
		
		titleBar.setOnRightButtonClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View paramView) {
				for (int i = 0; i < mCustoms.size(); i++) {
					boolean a = false;
					for (int j = 0; j < mCustoms.get(i).getmList().size(); j++) {
						boolean b =  mCustoms.get(i).getmList().get(j).isCheck();
						if( b ){
						 a = true;
							break;
						}
					}
					if( !a ){
						App.showShortToast(mCustoms.get(i).getSs()+"没有选择");
						return;
					}
					
				}
				StringBuffer ssString = new StringBuffer();
				for (int i = 0; i < mCustoms.size(); i++) {
					ssString.append(mCustoms.get(i).getSs());
					for (int j = 0; j < mCustoms.get(i).getmList().size(); j++) {
						if( mCustoms.get(i).getmList().get(j).isCheck() ){
							ssString.append("==>"+j+";");
								break;
							}
					}
				}
				App.showShortToast(ssString);
			}
		});
		
	
		
	}
	

}
